from commons.entity.po.travel import Travel
from commons.entity.query.travel_query import TravelQuery
from commons.mappers.base_mapper import BaseMapper
from travel_service.travel.mappers.travel_mapper import TravelMapper


class TravelService:
    def __init__(self):
        self.travel_mapper = TravelMapper()

    def test(self):
        # 使用对象作为过滤参数
        travel_query = TravelQuery(apply_date="2024-11-06")  # 这几种赋值方法都可以
        travel_query.travel_id = 1  # 这几种赋值方法都可以

        # 调用查询方法
        travels = self.travel_mapper.select_count(Travel, travel_query)

        # 输出查询结果
        print(travels)

        # 用户相关操作已经更改为出差
        print(self.get_travel_by_travel_id("T001"))

        travel_id = 1
        travel_bean = Travel()
        travel_bean.apply_date = "2024-11-07"  # 这几种赋值方法都可以
        self.update_travel_by_travel_id(travel_bean, travel_id)

        # travel_bean = Travel()  # 这几种赋值方法都可以
        # self.travel_mapper.insert_or_update(travel_bean)

    def find_list_by_param(self, travel_query=None):
        """
        业务逻辑：根据多个参数查询出差列表。
        :param travel_query: TravelQuery 实例，包含查询过滤条件。
        :return: 包含出差信息字典的列表。
        """
        print("TravelService find_list_by_param")
        print(travel_query)

        return self.travel_mapper.select_list(Travel, travel_query)

    def find_count_by_param(self, travel_query=None):
        """
        业务逻辑：根据多个参数统计出差数量。
        :param travel_query: TravelQuery 实例，包含查询过滤条件。
        :return: 符合条件的出差数量。
        """
        print("TravelService find_count_by_param")
        print(travel_query)

        return self.travel_mapper.select_count(Travel, travel_query)

    def get_travel_by_travel_id(self, travel_id):
        """
        业务逻辑：获取出差信息
        :param travel_id: str
        :return: 出差信息字典或 None
        """
        print("TravelService get_travel_by_id ")
        print(travel_id)

        return self.travel_mapper.select_by_travel_id(travel_id)

    def add(self, travel_bean):
        """
        业务逻辑：添加一个新的出差记录。
        :param travel_bean: Travel 类实例，包含出差信息。
        :return: 插入操作是否成功（True/False）。
        """
        print("TravelService add travel")
        print(travel_bean)

        return self.travel_mapper.insert(travel_bean)

    def add_batch(self, travel_list_bean):
        """
        业务逻辑：批量添加出差记录。
        :param travel_list_bean: 包含多个 Travel 类实例的列表，包含需要插入的出差信息。
        :return: 插入操作成功的出差数量。
        """
        print("TravelService add_batch travels")
        print(travel_list_bean)

        if not travel_list_bean:
            print("No travels to add. The input list is either null or empty.")
            return 0

        return self.travel_mapper.insert_batch(travel_list_bean)

    def add_or_update_batch(self, travel_list_bean):
        """
        业务逻辑：批量插入或更新出差信息。
        :param travel_list_bean: 包含多个 Travel 类实例的列表，包含需要插入或更新的出差信息。
        :return: 插入或更新成功的出差数量。
        """
        print("TravelService add_or_update_batch travels")
        print(travel_list_bean)

        if not travel_list_bean:
            print("No travels to add. The input list is either null or empty.")
            return 0

        return self.travel_mapper.insert_or_update_batch(Travel, travel_list_bean)

    def update_by_param(self, travel_bean, travel_query):
        """
        业务逻辑：根据多个条件更新出差信息。
        :param travel_bean: Travel 类实例或字典，包含需要更新的字段及其新值。
        :param travel_query: TravelQuery 类实例或字典，包含用于定位记录的过滤条件。
        :return: 更新成功的数量。
        """
        print("TravelService update_by_param")
        print(travel_bean)
        print(travel_query)

        return self.travel_mapper.update_by_params(Travel, travel_bean, travel_query)

    def delete_by_param(self, travel_query):
        """
        业务逻辑：根据多个条件删除出差信息。
        :param travel_query: TravelQuery 类实例或字典，包含用于定位记录的过滤条件。
        :return: 删除成功的数量。
        """
        print("TravelService delete_by_param")
        print(travel_query)

        return self.travel_mapper.delete_by_param(Travel, travel_query)

    def update_travel_by_travel_id(self, travel_bean, travel_id):
        """
        业务逻辑：根据 travel_id 更新出差信息
        :param travel_id: str
        :param travel_bean: Travel 类实例，包含需要更新的出差属性
        :return: 更新成功信息或错误信息
        """
        print("TravelService update_travel_by_id ")
        print(f"travel_id: {travel_id}, travel_bean: {travel_bean}")

        return self.travel_mapper.update_by_travel_id(travel_bean, travel_id)

    def delete_travel_by_travel_id(self, travel_id):
        """
        业务逻辑：根据 travel_id 删除出差记录
        :param travel_id: str
        :return: 删除成功或错误
        """
        print("TravelService delete_travel_by_id ")
        print(travel_id)

        return self.travel_mapper.delete_by_travel_id(travel_id)

    def get_all_travels_by_id_list(self, travel_id_list):
        """
        业务逻辑：根据出差ID列表获取多个出差信息。
        :param travel_id_list: 包含多个出差ID的列表。
        :return: 包含多个出差信息的列表。
        """
        print("TravelService get_all_travels_by_id_list")
        print(travel_id_list)

        if not travel_id_list:
            return []

        travels = []
        for travel_id in travel_id_list:
            travel = self.travel_mapper.select_by_travel_id(travel_id)
            if travel:
                travels.append(travel)

        return travels

    def get_final_travel_list_by_user_id(self, user_id):
        """
        业务逻辑：根据UserId获取筛选后的出差列表。
        :param user_id: 用户ID。
        :return: 筛选后的出差列表。
        """
        print("TravelService get_final_travel_list_by_user_id")
        print(user_id)

        if not user_id:
            return []

        # 调用Mapper方法查询数据
        travels = self.travel_mapper.select_final_by_user_id(user_id)

        # 返回筛选后的出差记录列表
        return travels

    def get_travel_by_user_id(self, user_id):
        """
        业务逻辑：获取出差信息
        :param user_id: str
        :return: 出差信息字典或 None
        """
        print("TravelService get_travel_by_user_id ")
        print(user_id)

        return self.travel_mapper.select_by_user_id(user_id)